package com.xasxt.book.app.utils;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.alibaba.druid.support.spring.stat.DruidStatInterceptor;

/**
 * 
 * 功能:阿里数据源公共类
 * 开发者:虫儿飞
 * 开发时间:2018年5月19日 下午1:50:08
 */
@Configuration
public class DruidDataSourceConfig  implements EnvironmentAware {

	private RelaxedPropertyResolver propertyResolver;
	public void setEnvironment(Environment env) {
		this.propertyResolver = new RelaxedPropertyResolver(env, "spring.datasource.");  
	}

	@Bean  
	public DataSource dataSource() {  
		DruidDataSource datasource = new DruidDataSource();  
		datasource.setUrl(propertyResolver.getProperty("url"));  
		datasource.setDriverClassName(propertyResolver.getProperty("driver-class-name"));  
		datasource.setUsername(propertyResolver.getProperty("username"));  
		datasource.setPassword(propertyResolver.getProperty("password"));  
		try {  
			datasource.setFilters("stat");  
		} catch (SQLException e) {  
			e.printStackTrace();  
		}  
		return datasource;  
	} 


	@Bean  
	public ServletRegistrationBean druidServlet() {  
		ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();  
		servletRegistrationBean.setServlet(new StatViewServlet());  
		servletRegistrationBean.addUrlMappings("/druid/*");  
		Map<String, String> initParameters = new HashMap<String, String>();  
		initParameters.put("resetEnable", "false");
		initParameters.put("allow", "127.0.0.1");   

		servletRegistrationBean.setInitParameters(initParameters);  
		return servletRegistrationBean; 

	}


	@Bean  
	public FilterRegistrationBean filterRegistrationBean() {  
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();  
		filterRegistrationBean.setFilter(new WebStatFilter());  
		filterRegistrationBean.addUrlPatterns("/*");  
		filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");  
		return filterRegistrationBean;  
	}  


	@Bean(value = "druid-stat-interceptor")  
	public DruidStatInterceptor DruidStatInterceptor() {  
		DruidStatInterceptor druidStatInterceptor = new DruidStatInterceptor();  
		return druidStatInterceptor;  
	}  

	@Bean  
	public BeanNameAutoProxyCreator beanNameAutoProxyCreator() {  
		BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();  
		beanNameAutoProxyCreator.setProxyTargetClass(true);   
		beanNameAutoProxyCreator.setInterceptorNames("druid-stat-interceptor");  
		return beanNameAutoProxyCreator;  
	} 




}
